package com.vitas.coin.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.vitas.coin.data.bean.MusicType;
import com.vitas.coin.databinding.ItemPlaylistBinding;
import com.vitas.coin.ui.dialog.DetailJumper;
import com.vitas.coin.ui.fm.ListFrag;

import java.util.List;

public class TypeListAdapter extends RecyclerView.Adapter {

    List<MusicType> list;
    LayoutInflater inflater;
    DetailJumper fragment;

    public TypeListAdapter(DetailJumper frag, Context context, List list) {
        inflater = LayoutInflater.from(context);
        this.list = list;
        fragment = frag;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemPlaylistBinding binding = ItemPlaylistBinding.inflate(inflater, parent, false);
        return new Holder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof Holder) {
            ((Holder) holder).bind(position);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        ItemPlaylistBinding binding;

        Holder(ItemPlaylistBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(int p) {
            MusicType item = list.get(p);
            binding.tvName.setText(item.name);
            binding.tvCount.setText(item.getNum() + "个");
            itemView.setOnClickListener(v -> fragment.toList(getAdapterPosition()));
        }
    }
}
